package s16.ch19.ex2;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MyAnnotations {

	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {

		List<Person> persons = new ArrayList<Person>();

		persons.add(new Person("Mark", "Kelly", "AOL"));
		persons.add(new Person("Kris", "McDonald", "Google"));

		processPersons(persons);
		
		for (Person person : persons) {
			System.out.println(person);
		}

	}

	private static void processPersons(List<Person> persons) throws IllegalArgumentException, IllegalAccessException {
		Field[] declaredFields = Person.class.getDeclaredFields();
		for (Field field : declaredFields) {
			Separator annotation = field.getAnnotation(Separator.class);
			System.out.println(annotation);
			field.setAccessible(true);
			
			if (field.getType() == String.class && annotation != null) {

				String ending = annotation.ending();
				String trailing = annotation.trailing();

				for (Person person : persons) {
					String string = (String) field.get(person);
					field.set(person, trailing + string + ending); 
				}

			}
		}
	}

}
